﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace KCL.Code.Data
{
    public class InputFinishedProductDAO : BaseDAO
    {
        public bool UpdateQuantity(string orderMaterialIdList, string orderMaterialFinishedQuantity, int orderId)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("sp_UpdateFinishedProduct", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@orderMaterialIdList", orderMaterialIdList));
                command.Parameters.Add(new SqlParameter("@orderMaterialFinishedQuantity", orderMaterialFinishedQuantity));
                command.Parameters.Add(new SqlParameter("@orderId", orderId));

                try
                {
                    connection.Open();
                    return (command.ExecuteNonQuery() != 0);
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public DataTable GetUnifinishedProductID()
        {
            using (SqlConnection connection = GetConnection())
            {
                string sqlStatement = string.Format("SELECT [id] FROM [Order] WHERE [isFinished] = 0 ORDER BY [createdDate]");
                SqlCommand command = new SqlCommand(sqlStatement, connection);

                SqlDataAdapter dataAdap = new SqlDataAdapter(command);
                DataTable data = new DataTable();               

                try
                {
                    connection.Open();
                    dataAdap.Fill(data);
                    return data;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
    }
}